/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */

/*
 * EkranEdycjiWizyt.java
 *
 * Created on 2011-11-20, 22:56:12
 */
package przychodnia;

import DTO.Pacjenci;
import DTO.Pracownicy;
import DTO.PracownicyZabiegi;
import DTO.Wizyty;
import DTO.Zabiegi;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;
import java.util.logging.Level;
import java.util.logging.Logger;
import javax.swing.DefaultComboBoxModel;
import org.hibernate.Session;
import org.hibernate.criterion.Restrictions;

/**
 *
 * @author konp
 */
public class EkranEdycjiWizyt extends javax.swing.JFrame
{
    
    private Session ses = null;
    private Long id;
    private Pacjenci pacjent = null;
    //private Zabiegi zaby = null;
    private Wizyty wizyta;
    final ArrayList<Pracownicy> doctorsList = new ArrayList<Pracownicy>();
    final ArrayList<Zabiegi> treatmentList = new ArrayList<Zabiegi>();
    private int add_edit;
    private Zabiegi nowy = null;

    /** Creates new form EkranEdycjiWizyt */
    public EkranEdycjiWizyt(Session xses, Long xid, int xadd_edit)
    {
        ses = xses;
        id = xid;
        add_edit = xadd_edit;

        initComponents();
        setDefaultCloseOperation(DISPOSE_ON_CLOSE);
        getRootPane().setDefaultButton(okButton);
        
        if (add_edit == 0)
            pacjent = (Pacjenci) ses.createCriteria(Pacjenci.class).add(Restrictions.eq("id", id)).list().get(0);
        else
        {
            wizyta = (Wizyty) ses.createCriteria(Wizyty.class).add(Restrictions.eq("id", id)).list().get(0);
            pacjent = (Pacjenci) wizyta.getPacjent();
        }
            
        
        patientLabel.setText(pacjent.getNazwisko() + " " + pacjent.getImie());
        peselLabel.setText(pacjent.getPESEL() + "");

        // wypisz wszystkich lekarzy i zostaw pierwsze pole puste
        List pracs = ses.createQuery("from Pracownicy").list();
        doctorComboBox.addItem("");
        for (int i = 0; i < pracs.size(); ++i)
        {
            Pracownicy prac = (Pracownicy) pracs.get(i);
            doctorsList.add(prac);
            doctorComboBox.addItem(prac.getNazwisko() + " " + prac.getImie());
        }
        
        doctorComboBox.addActionListener(new ActionListener()
        {
            
            public void actionPerformed(ActionEvent e)
            {
                DefaultComboBoxModel theModel = (DefaultComboBoxModel) treatmentComboBox.getModel();
                theModel.removeAllElements();
                
                //theModel = (DefaultComboBoxModel) ComboBox.getModel();
                //theModel.removeAllElements();
                
                
                Pracownicy nowy = doctorsList.get(doctorComboBox.getSelectedIndex() - 1);
                List zabiegi = ses.createCriteria(PracownicyZabiegi.class).list();
                treatmentList.clear();
                for (int i = 0; i < zabiegi.size(); ++i)
                {
                    PracownicyZabiegi zab = (PracownicyZabiegi) zabiegi.get(i);
                    
                    if (nowy == zab.getPracownik())
                    {
                        treatmentComboBox.addItem(zab.getZabieg().getNazwa());
                        treatmentList.add(zab.getZabieg());
                    }
                }
                
            }
        });
        
        /*treatmentComboBox.addActionListener(new ActionListener()
        {
            
            public void actionPerformed(ActionEvent e)
            {
                
                if (treatmentComboBox.getSelectedIndex() > -1)
                {
                    nowy = treatmentList.get(treatmentComboBox.getSelectedIndex());
                    //descLabel.setText(nowy.getOpis());
                }
            }
        });
*/
        // wypelnij date i pokoje
        for (int i = 1; i <= 31; ++i)
        {
            dayComboBox.addItem(i);
        }
        for (int i = 1; i <= 12; ++i)
        {
            monthComboBox.addItem(i);
        }
        for (int i = 2011; i <= 2012; ++i)
        {
            yearComboBox.addItem(i);
        }
        
        for (int i = 310; i <= 330; ++i)
        {
            roomComboBox.addItem(i);
        }
        
        // wypelnij comboboxy
        if (add_edit == 1)
        {
            SimpleDateFormat formatter = new SimpleDateFormat("dd/MM/yyyy");
            String out = formatter.format(wizyta.getData());
            String[] items = out.split("/");
            int itemsI[] = new int[3];
            
            itemsI[0] = Integer.parseInt(items[0]);
            itemsI[1] = Integer.parseInt(items[1]);
            itemsI[2] = Integer.parseInt(items[2]);

            doctorComboBox.setSelectedItem(wizyta.getLekarz().getNazwisko() + " " + wizyta.getLekarz().getImie());
            treatmentComboBox.setSelectedItem(wizyta.getZabieg().getNazwa());
            dayComboBox.setSelectedItem(itemsI[0]);
            monthComboBox.setSelectedItem(itemsI[1]);
            yearComboBox.setSelectedItem(itemsI[2]);
            attenTextField.setText(wizyta.getUwagi());
        }
        
    }

    /** This method is called from within the constructor to
     * initialize the form.
     * WARNING: Do NOT modify this code. The content of this method is
     * always regenerated by the Form Editor.
     */
    @SuppressWarnings("unchecked")
    // <editor-fold defaultstate="collapsed" desc="Generated Code">//GEN-BEGIN:initComponents
    private void initComponents() {

        cancelButton = new javax.swing.JButton();
        okButton = new javax.swing.JButton();
        surnameLabel = new javax.swing.JLabel();
        jLabel1 = new javax.swing.JLabel();
        jLabel3 = new javax.swing.JLabel();
        jLabel4 = new javax.swing.JLabel();
        jLabel5 = new javax.swing.JLabel();
        doctorComboBox = new javax.swing.JComboBox();
        dayComboBox = new javax.swing.JComboBox();
        roomComboBox = new javax.swing.JComboBox();
        patientLabel = new javax.swing.JLabel();
        peselLabel = new javax.swing.JLabel();
        monthComboBox = new javax.swing.JComboBox();
        yearComboBox = new javax.swing.JComboBox();
        treatmentLabel = new javax.swing.JLabel();
        treatmentComboBox = new javax.swing.JComboBox();
        attenLabel = new javax.swing.JLabel();
        attenTextField = new javax.swing.JTextField();

        setDefaultCloseOperation(javax.swing.WindowConstants.EXIT_ON_CLOSE);
        setName("Form"); // NOI18N

        org.jdesktop.application.ResourceMap resourceMap = org.jdesktop.application.Application.getInstance(przychodnia.Main.class).getContext().getResourceMap(EkranEdycjiWizyt.class);
        cancelButton.setText(resourceMap.getString("cancelButton.text")); // NOI18N
        cancelButton.setName("cancelButton"); // NOI18N
        cancelButton.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                cancelButtonActionPerformed(evt);
            }
        });

        okButton.setText(resourceMap.getString("okButton.text")); // NOI18N
        okButton.setName("okButton"); // NOI18N
        okButton.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                okButtonActionPerformed(evt);
            }
        });

        surnameLabel.setText(resourceMap.getString("surnameLabel.text")); // NOI18N
        surnameLabel.setName("surnameLabel"); // NOI18N

        jLabel1.setText(resourceMap.getString("jLabel1.text")); // NOI18N
        jLabel1.setName("jLabel1"); // NOI18N

        jLabel3.setText(resourceMap.getString("jLabel3.text")); // NOI18N
        jLabel3.setName("jLabel3"); // NOI18N

        jLabel4.setText(resourceMap.getString("jLabel4.text")); // NOI18N
        jLabel4.setName("jLabel4"); // NOI18N

        jLabel5.setText(resourceMap.getString("jLabel5.text")); // NOI18N
        jLabel5.setName("jLabel5"); // NOI18N

        doctorComboBox.setName("doctorComboBox"); // NOI18N

        dayComboBox.setName("dayComboBox"); // NOI18N

        roomComboBox.setName("roomComboBox"); // NOI18N

        patientLabel.setText(resourceMap.getString("patientLabel.text")); // NOI18N
        patientLabel.setName("patientLabel"); // NOI18N

        peselLabel.setText(resourceMap.getString("peselLabel.text")); // NOI18N
        peselLabel.setName("peselLabel"); // NOI18N

        monthComboBox.setName("monthComboBox"); // NOI18N

        yearComboBox.setName("yearComboBox"); // NOI18N

        treatmentLabel.setText(resourceMap.getString("treatmentLabel.text")); // NOI18N
        treatmentLabel.setName("treatmentLabel"); // NOI18N

        treatmentComboBox.setName("treatmentComboBox"); // NOI18N

        attenLabel.setText(resourceMap.getString("attenLabel.text")); // NOI18N
        attenLabel.setName("attenLabel"); // NOI18N

        attenTextField.setText(resourceMap.getString("attenTextField.text")); // NOI18N
        attenTextField.setName("attenTextField"); // NOI18N

        javax.swing.GroupLayout layout = new javax.swing.GroupLayout(getContentPane());
        getContentPane().setLayout(layout);
        layout.setHorizontalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(layout.createSequentialGroup()
                .addContainerGap()
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.TRAILING)
                    .addGroup(layout.createSequentialGroup()
                        .addGap(23, 23, 23)
                        .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.TRAILING)
                            .addComponent(jLabel1)
                            .addComponent(jLabel3)))
                    .addComponent(jLabel4)
                    .addComponent(treatmentLabel)
                    .addComponent(jLabel5)
                    .addComponent(attenLabel)
                    .addComponent(okButton, javax.swing.GroupLayout.PREFERRED_SIZE, 75, javax.swing.GroupLayout.PREFERRED_SIZE)
                    .addComponent(surnameLabel))
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addGroup(layout.createSequentialGroup()
                        .addComponent(peselLabel, javax.swing.GroupLayout.DEFAULT_SIZE, 155, Short.MAX_VALUE)
                        .addContainerGap())
                    .addGroup(layout.createSequentialGroup()
                        .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING, false)
                            .addComponent(doctorComboBox, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                            .addComponent(treatmentComboBox, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                            .addComponent(roomComboBox, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                            .addComponent(cancelButton)
                            .addComponent(attenTextField)
                            .addGroup(layout.createSequentialGroup()
                                .addComponent(dayComboBox, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                                .addComponent(monthComboBox, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                                .addComponent(yearComboBox, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)))
                        .addGap(59, 59, 59))
                    .addGroup(javax.swing.GroupLayout.Alignment.TRAILING, layout.createSequentialGroup()
                        .addComponent(patientLabel, javax.swing.GroupLayout.DEFAULT_SIZE, 155, Short.MAX_VALUE)
                        .addContainerGap())))
        );
        layout.setVerticalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(javax.swing.GroupLayout.Alignment.TRAILING, layout.createSequentialGroup()
                .addContainerGap()
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addComponent(jLabel1)
                    .addComponent(patientLabel, javax.swing.GroupLayout.PREFERRED_SIZE, 17, javax.swing.GroupLayout.PREFERRED_SIZE))
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.TRAILING)
                    .addComponent(jLabel3)
                    .addComponent(peselLabel, javax.swing.GroupLayout.PREFERRED_SIZE, 15, javax.swing.GroupLayout.PREFERRED_SIZE))
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(doctorComboBox, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                    .addComponent(surnameLabel))
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(treatmentLabel)
                    .addComponent(treatmentComboBox, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(jLabel4)
                    .addComponent(dayComboBox, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                    .addComponent(monthComboBox, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                    .addComponent(yearComboBox, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(jLabel5)
                    .addComponent(roomComboBox, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(attenLabel)
                    .addComponent(attenTextField, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(okButton)
                    .addComponent(cancelButton))
                .addGap(29, 29, 29))
        );

        pack();
    }// </editor-fold>//GEN-END:initComponents

    private void cancelButtonActionPerformed(java.awt.event.ActionEvent evt)//GEN-FIRST:event_cancelButtonActionPerformed
    {//GEN-HEADEREND:event_cancelButtonActionPerformed
        dispose();
    }//GEN-LAST:event_cancelButtonActionPerformed
    
    private void okButtonActionPerformed(java.awt.event.ActionEvent evt)//GEN-FIRST:event_okButtonActionPerformed
    {//GEN-HEADEREND:event_okButtonActionPerformed
        ses.beginTransaction();

        if (add_edit == 0)
            wizyta = new Wizyty();
        wizyta.setPacjent(pacjent);
        
        SimpleDateFormat format = new SimpleDateFormat("dd/MM/yyyy");
        Date dat;
        try
        {
            dat = format.parse(dayComboBox.getSelectedItem() + "/" + monthComboBox.getSelectedItem() + "/" + yearComboBox.getSelectedItem());
            wizyta.setData(dat);
        } catch (ParseException ex)
        {
            Logger.getLogger(EkranEdycjiWizyt.class.getName()).log(Level.SEVERE, null, ex);
        }
        
        wizyta.setLekarz(doctorsList.get(doctorComboBox.getSelectedIndex() - 1));
        nowy = treatmentList.get(treatmentComboBox.getSelectedIndex());
        wizyta.setZabieg(nowy);
        wizyta.setUwagi(attenTextField.getText());
        ses.save(wizyta);
        ses.getTransaction().commit();
        
        dispose();
    }//GEN-LAST:event_okButtonActionPerformed

    /**
     * @param args the command line arguments
     */
    public static void main(String args[])
    {
        /* Set the Nimbus look and feel */
        //<editor-fold defaultstate="collapsed" desc=" Look and feel setting code (optional) ">
        /* If Nimbus (introduced in Java SE 6) is not available, stay with the default look and feel.
         * For details see http://download.oracle.com/javase/tutorial/uiswing/lookandfeel/plaf.html 
         */
        try
        {
            for (javax.swing.UIManager.LookAndFeelInfo info : javax.swing.UIManager.getInstalledLookAndFeels())
            {
                if ("Nimbus".equals(info.getName()))
                {
                    javax.swing.UIManager.setLookAndFeel(info.getClassName());
                    break;
                }
            }
        } catch (ClassNotFoundException ex)
        {
            java.util.logging.Logger.getLogger(EkranEdycjiWizyt.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
        } catch (InstantiationException ex)
        {
            java.util.logging.Logger.getLogger(EkranEdycjiWizyt.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
        } catch (IllegalAccessException ex)
        {
            java.util.logging.Logger.getLogger(EkranEdycjiWizyt.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
        } catch (javax.swing.UnsupportedLookAndFeelException ex)
        {
            java.util.logging.Logger.getLogger(EkranEdycjiWizyt.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
        }
        //</editor-fold>

        /* Create and display the form */
        java.awt.EventQueue.invokeLater(new Runnable()
        {
            
            public void run()
            {
//                new EkranEdycjiWizyt().setVisible(true);
            }
        });
    }
    // Variables declaration - do not modify//GEN-BEGIN:variables
    private javax.swing.JLabel attenLabel;
    private javax.swing.JTextField attenTextField;
    private javax.swing.JButton cancelButton;
    private javax.swing.JComboBox dayComboBox;
    private javax.swing.JComboBox doctorComboBox;
    private javax.swing.JLabel jLabel1;
    private javax.swing.JLabel jLabel3;
    private javax.swing.JLabel jLabel4;
    private javax.swing.JLabel jLabel5;
    private javax.swing.JComboBox monthComboBox;
    private javax.swing.JButton okButton;
    private javax.swing.JLabel patientLabel;
    private javax.swing.JLabel peselLabel;
    private javax.swing.JComboBox roomComboBox;
    private javax.swing.JLabel surnameLabel;
    private javax.swing.JComboBox treatmentComboBox;
    private javax.swing.JLabel treatmentLabel;
    private javax.swing.JComboBox yearComboBox;
    // End of variables declaration//GEN-END:variables
}
